/**  
 * @Title: RoleServiceImpl.java
 * @Package com.xzit.tmms.service.user.impl
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月24日 下午2:43:29
 * @version V1.0  
 */
package com.xzit.tmms.service.user.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xzit.tmms.bean.user.Right;
import com.xzit.tmms.bean.user.Role;
import com.xzit.tmms.bean.user.RoleRight;
import com.xzit.tmms.common.exception.ServiceException;
import com.xzit.tmms.common.page.Page;
import com.xzit.tmms.common.util.CommonUtil;
import com.xzit.tmms.mapper.user.RoleMapper;
import com.xzit.tmms.mapper.user.RoleRightMapper;
import com.xzit.tmms.service.user.RoleService;

/**
 * @ClassName: RoleServiceImpl
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月24日 下午2:43:29
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

	@Resource
	private RoleMapper roleMapper;

	@Resource
	private RoleRightMapper roleRightMapper;

	@Override
	public List<Map<String, Object>> getRoleList() throws ServiceException {
		try {
			return roleMapper.getRoleList();
		} catch (Exception e) {
			throw new ServiceException("加载角色列表失败");
		}
	}

	@Override
	public Page<Role> queryRole(Page<Role> page) throws ServiceException {
		try {
			roleMapper.queryRole(page);
			return page;
		} catch (Exception e) {
			throw new ServiceException("加载角色时发生错误");
		}
	}

	@Override
	public Role getRoleById(Integer roleId) throws ServiceException {
		try {
			if (CommonUtil.isNotEmpty(roleId)) {
				return roleMapper.getById(roleId);
			}
		} catch (Exception e) {
			throw new ServiceException("加载角色时发生错误");
		}
		return null;
	}

	@Override
	public List<Right> getRightsByRole(Integer roleId) throws ServiceException {
		try {
			if (CommonUtil.isNotEmpty(roleId)) {
				return roleMapper.getRightsByRole(roleId);
			}
		} catch (Exception e) {
			throw new ServiceException("加载权限时发生错误");
		}
		return null;
	}

	@Override
	public Integer updateRoleRights(Integer roleId, List<Integer> idList) throws ServiceException {
		try {
			if (CommonUtil.isNotEmpty(roleId)) {
				// 删除已有权限
				roleRightMapper.deleteRoleRights(roleId);
				// 新增权限
				if (CommonUtil.isNotEmpty(idList)) {
					for (Integer rightId : idList) {
						RoleRight roleRight = new RoleRight();
						roleRight.setRightId(rightId);
						roleRight.setRoleId(roleId);
						roleRightMapper.save(roleRight);
					}

				}
				return 1;
			}
		} catch (Exception e) {
			throw new ServiceException("分配权限时发生错误");
		}
		return 0;
	}

	@Override
	public Integer getRoleIdByRoleValue(String roleValue) throws ServiceException {
		try {
			if (CommonUtil.isNotEmpty(roleValue)) {
				return roleMapper.getRoleId(roleValue);
			}
		} catch (Exception e) {
			throw new ServiceException("查询数据时发生错误");
		}
		return null;
	}

	@Override
	public String getRoleValueByAdminId(Integer adminId) throws ServiceException {
		try {
			if (CommonUtil.isNotEmpty(adminId)) {
				Map<String, Object> role = roleMapper.getRoleValueByAdminId(adminId);
				if (CommonUtil.isNotEmpty(role)) {
					return role.get("roleValue").toString();
				}
			}
		} catch (Exception e) {
			throw new ServiceException("查询数据时发生错误");
		}
		return null;
	}

	@Override
	public List<Right> getAllRoles() throws ServiceException {
		try {
			return roleMapper.getAll();
		} catch (Exception e) {
			throw new ServiceException("查询系统角色时发生错误");
		}
	}
}
